package com.seis_645.appTesting;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;

public class SaveSalesPersonActivity extends Activity {
	
	private EditText fname, lname;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.add_salesperson);

		Button cancel = (Button) findViewById(R.id.btn_cancel_sales);
		Button save = (Button) findViewById(R.id.btn_save_sales);
		OnTouchListener cancelListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					finish();
					// return true;
				}
				return false;
			}
		};
		
		OnTouchListener saveListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_UP) {
					addToDatabase();
					finish();
				}
				return false;
			}
		};
		
		cancel.setOnTouchListener(cancelListener);
		save.setOnTouchListener(saveListener);
	}
	
	private void addToDatabase() {
		fname = (EditText) findViewById(R.id.fname);
		lname = (EditText) findViewById(R.id.lname);
		
		SalesPersonDataSource datasource = new SalesPersonDataSource(this);
		
		datasource.open();
		datasource.create_salesperson(1, fname.getText().toString(), lname.getText().toString());
		datasource.close();
		
		return;
	}

}
